package com.ef.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.AdChannelDao;
import com.ef.domain.AdChannel;
import com.ef.domain.Team;
import com.ef.service.AdChannelService;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class AdChannelServiceImpl extends BaseServiceImpl implements
		AdChannelService {

	private AdChannelDao adChannelDao;

	public void delete(AdChannel entity) {
		entity.setStatus(StatusType.INVALID);
		adChannelDao.update(entity);

	}

	public PaginationSupport<AdChannel> findByExample(
			CustomExample<AdChannel> example, int startIndex, int pageSize) {

		return adChannelDao.findPageByExample(example, startIndex, pageSize);
	}

	public AdChannel findById(Long id) {

		return adChannelDao.findById(id);
	}

	public void saveOrUpdate(AdChannel entity) {

		adChannelDao.clear();
		adChannelDao.saveOrUpdate(entity);
	}

	public List<AdChannel> findAll() {
		DetachedCriteria criteria = DetachedCriteria.forClass(AdChannel.class);
		criteria.add(Restrictions.eq("status", StatusType.VALID));
		return adChannelDao.findAllByCriteria(criteria);
	}

	public AdChannelDao getAdChannelDao() {
		return adChannelDao;
	}

	public void setAdChannelDao(AdChannelDao adChannelDao) {
		this.adChannelDao = adChannelDao;
	}

	public List<AdChannel> findByAnything(String qitem, int max) {
		StringBuffer hql = new StringBuffer(
				"From AdChannel where (lower(name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(pyname) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and status='VALID' ");
		return adChannelDao.findMultiAll(hql.toString(), 0, max);
//		DetachedCriteria criterion = DetachedCriteria.forClass(AdChannel.class);
//		Disjunction or = Restrictions.disjunction();
//		Property myProperty = Property.forName("name");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		myProperty = Property.forName("pyname");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		criterion.add(or);
//		criterion.add(Restrictions.eq("status", StatusType.VALID));
//		return adChannelDao.findAllByCriteria(criterion, max);
	}
	
	public List<AdChannel> findByAnythingWithTeam(Team team,String qitem, int max) {
		StringBuffer hql = new StringBuffer(
				"From AdChannel where (lower(name) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%' or lower(pyname) like '%");
		hql.append(qitem.trim().toLowerCase());
		hql.append("%') and status='VALID' and team_id=");
		hql.append(team.getId());
		return adChannelDao.findMultiAll(hql.toString(), 0, max);
//		DetachedCriteria criterion = DetachedCriteria.forClass(AdChannel.class);
//		Disjunction or = Restrictions.disjunction();
//		Property myProperty = Property.forName("name");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		myProperty = Property.forName("pyname");
//		or.add(myProperty.like("%" + qitem.toLowerCase() + "%"));
//		or.add(myProperty.like("%" + qitem.toUpperCase() + "%"));
//		criterion.add(or);
//		criterion.add(Restrictions.eq("status", StatusType.VALID));
//		return adChannelDao.findAllByCriteria(criterion, max);
	}
}
